package tut.collection.code;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MapTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HashMap<EmployeeKey, Employee> hashMap = new HashMap<MapTest.EmployeeKey, MapTest.Employee>();
		MapTest m = new MapTest();

		hashMap.put(m.new EmployeeKey(1, "one"), m.new Employee(2, "2"));
		hashMap.put(m.new EmployeeKey(1, "one"), m.new Employee(2, "2"));
		m.printMap(hashMap);

		EmployeeKey eKey = m.new EmployeeKey(11, "one11");
		EmployeeKey eKey1 = m.new EmployeeKey(11, "one11");
		hashMap.put(eKey, m.new Employee(2, "2"));
		hashMap.put(eKey1, m.new Employee(2, "2"));
		m.printMap(hashMap);


		System.out.println(m.new EmployeeKey(1, "one").equals(m.new EmployeeKey(1, "one")));
	}

	public class Employee {
		int age;
		String name = null;
		public Employee(int age, String name){
			this.age = age;
			this.name = name;
		}
	}

	public class EmployeeKey {
		int keyId;
		String keyName = null;
		public EmployeeKey(int keyId, String keyName){
			this.keyId = keyId;
			this.keyName = keyName;
		}

///*
  		public boolean equals(Object o) {
			if(o == null) return false;
			if(!(o instanceof EmployeeKey)) return false;

			EmployeeKey employeeKey = (EmployeeKey) o;
			if(employeeKey.keyName == null || this.keyName == null) return false;

			return this.keyId == employeeKey.keyId && this.keyName.equals(employeeKey.keyName);
		}

		public int hashcode() {
			return this.toString().hashCode();
		}

		public String toString(){
			return this.keyId + ", " + this.keyName;
		}
//*/
	}

	public void printMap(Map<EmployeeKey, Employee> map) {
		Iterator<EmployeeKey> i = map.keySet().iterator();
		while (i.hasNext()) {
			EmployeeKey oKey = (EmployeeKey) i.next();
			Employee oVal = (Employee) map.get(oKey);
			System.out.println(oKey.keyId +" " + oKey.keyName + ", " + oVal.age + " " + oVal.name );
		}

		System.out.println("===============");
	}

}
